package com.survivor.menu;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.survivor.activity.GameActivity;
import com.survivor.activity.GameInterface;

import android.app.Activity;
import android.content.Context;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.view.MotionEvent;

public class MenuRenderer implements Renderer {

	private AllContent content;
	private GameInterface iGame;

	public MenuRenderer(Context context, GameInterface iGame) {
		// TODO Auto-generated constructor stub
		this.iGame = iGame;
		content = new AllContent(context);
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

		content.doDraw(gl);

		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 45f, (float) width / height, .1f, 10f);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		gl.glClearColor(0, 0, 0, 1f);
		gl.glEnable(GL10.GL_TEXTURE_2D);

		content.loadTextures(gl);

		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glClearDepthf(1f);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glShadeModel(GL10.GL_SMOOTH);
	}
	
	public void clickBack()
	{
		if(content.curView == AllContent.MAIN_VIWE)
			iGame.exitGame();
		else
			content.curView = AllContent.MAIN_VIWE;
	}

	public void onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		int action = event.getAction();

		if (content.curView == AllContent.MAIN_VIWE) {
			switch (action) {
			case MotionEvent.ACTION_DOWN:
				if (content.button.rectPlay.contains(x, y))
				{
					content.button.isClickPlay = true;
					iGame.playClick();
				}
				if (content.button.rectExit.contains(x, y))
				{
					iGame.playClick();
					content.button.isClickExit = true;
				}
				if (content.button.rectHelp.contains(x, y))
				{
					iGame.playClick();
					content.button.isClickHelp = true;
				}
				if (content.button.rectRank.contains(x, y))
				{
					iGame.playClick();
					content.button.isClickRank = true;
				}
					

				break;
			case MotionEvent.ACTION_UP:
				if (content.button.rectPlay.contains(x, y))
					content.curView = AllContent.SETTING_VIEW;
				content.button.isClickPlay = false;
				if (content.button.rectExit.contains(x, y))
					iGame.exitGame();
				content.button.isClickExit = false;
				if (content.button.rectHelp.contains(x, y))
					content.curView =  AllContent.HELP_VIEW;
				content.button.isClickHelp = false;
				if (content.button.rectRank.contains(x, y))
					content.curView = AllContent.RANDLIST_VIEW;
				content.button.isClickRank = false;
				break;
			}
		}
		else if(content.curView == AllContent.SETTING_VIEW)
		{
			switch (action) {
			case MotionEvent.ACTION_DOWN:
				if(content.button.rectEasy.contains(x, y))
				{
					iGame.playClick();
					content.button.isClickEasy = true;
				}
				if(content.button.rectNormal.contains(x, y))
				{
					iGame.playClick();
					content.button.isClickNormal = true;
				}
				if(content.button.rectHard.contains(x, y))
				{
					iGame.playClick();
					content.button.isClickHard = true;
				}
				break;
			case MotionEvent.ACTION_UP:
				if(content.button.rectEasy.contains(x, y))
				{
					iGame.setDiff(GameActivity.EASY);
					iGame.gotoGameView();
					content.curView = AllContent.MAIN_VIWE;
				}
				content.button.isClickEasy = false;
				if(content.button.rectNormal.contains(x, y))
				{
					iGame.setDiff(GameActivity.NORMAL);
					iGame.gotoGameView();
					content.curView = AllContent.MAIN_VIWE;
				}
				content.button.isClickNormal = false;
				if(content.button.rectHard.contains(x, y))
				{
					iGame.setDiff(GameActivity.HARD);
					iGame.gotoGameView();
					content.curView = AllContent.MAIN_VIWE;
				}
				content.button.isClickHard = false;
				break;
			}
		}
		else
		{
			switch (action) {
			case MotionEvent.ACTION_DOWN:
				if(content.button.rectBack.contains(x, y))
				{
					iGame.playClick();
					content.button.isClickBack = true;
				}
				break;
			case MotionEvent.ACTION_UP:
				if(content.button.rectBack.contains(x, y))
					content.curView = AllContent.MAIN_VIWE;
				content.button.isClickBack = false;
				break;
			}
		}
	}
}
